package cn.edu.cdu.health.controller;

import cn.edu.cdu.health.entity.Appointment;
import cn.edu.cdu.health.entity.AppointmentPageDto;
import cn.edu.cdu.health.common.R;
import cn.edu.cdu.health.service.AppointmentService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/appointment")
public class AppointmentController {

    @Autowired
    private AppointmentService appointmentService;

    // 分页查询预约
    @PostMapping("/page")
    public R pageQuery(@RequestBody AppointmentPageDto dto) {
        Page<Appointment> page = appointmentService.pageQuery(dto);
        return R.ok(page);
    }

    // 查询单个预约
    @GetMapping("/{id}")
    public R getById(@PathVariable Long id) {
        Appointment appointment = appointmentService.getById(id);
        if (appointment == null) {
            return R.error("未找到对应的预约信息");
        }
        return R.ok(appointment);
    }

    // 更新预约状态
    @PutMapping("/status/{id}")
    public R updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        if (status < 1 || status > 4) {
            return R.error("无效的状态值，状态值必须在1-4之间");
        }

        boolean result = appointmentService.updateStatus(id, status);
        if (result) {
            return R.ok("状态更新成功");
        } else {
            return R.error("状态更新失败，未找到对应的预约信息");
        }
    }
}